<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Authenticatable;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class Member extends Model implements AuthenticatableContract, AuthorizableContract, CanResetPasswordContract {
	
	use Authenticatable, Authorizable, CanResetPassword;
	
	protected $guarded = array('id');
	
	public function administrator() {
		return $this->hasOne('App\Models\Administrator');
	}
	
	public function bookshelves() {
		return $this->hasMany('App\Models\Bookshelf');
	}
	
	public function isInBookshelves($bookId) {
		$bookshelf = Bookshelf::where('book_id', $bookId)
				->where('member_id', $this->id)
				->first();
		return is_null($bookshelf) ? false : true;
	}
	
	public function avatarUrl() {
		$avatar = $this->avatar;
		if (!starts_with($this->avatar, 'http')) {
			$avatar = asset('/uploads/images/'. $this->avatar);
		}
		return $avatar;
	}
	
	const GENDER_MALE = 'male';
	const GENDER_FAMALE = 'famale';
	const GENDER_SECRET = 'secret';
	
	public static $genders = [
		self::GENDER_MALE => '男',
		self::GENDER_FAMALE => '女',
		self::GENDER_SECRET => '保密'
	];
	
	public function gender_ex() {
		return self::$genders[$this->gender];
	}
	
	public static $rules = [
		'nickname' => 'required',
		'cellphone' => 'required',
		'password' => 'required',
	];
	
	public static $msgs = [
		'nickname.required' => '昵称不能为空',
		'cellphone.required' => '手机号不能为空',
		'password.required' => '请输入密码',
	];
	
}
